﻿using Microsoft.EntityFrameworkCore;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Zhy.DAL;
using Zhy.DAL.Models;

namespace WPFEFCoreTest.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        #region 字段
        ZhyDbContext context = new ZhyDbContext();
        #endregion

        /// <summary>
        /// 初始化
        /// </summary>
        public MainWindowViewModel()
        {

        }

        /// <summary>
        /// 新增按钮
        /// </summary>
        public DelegateCommand<object> AddCommand
        {
            get
            {
                return new DelegateCommand<object>(o =>
                {  // 执行Command绑定事件
                    Company company = new Company()
                    {
                        CreateTime = DateTime.Now,
                        LastModifierId = 1,
                        LastModifyTime = DateTime.Now,
                        Name = "朝夕教育科技有限公司",
                        CreatorId = 1
                    };
                    //_DbContext.Add(company);
                    //_DbContext.SaveChanges();
                    using (ZhyDbContext context = new ZhyDbContext())
                    {
                        context.Add(company);
                        context.SaveChanges();   //提交保存 
                    }

                }, o =>
                { // 执行条件检测事件
                    return true; //决定了目标对象的可用性 true可用 false不可用
                });
            }
        }

        /// <summary>
        /// 新增按钮
        /// </summary>
        public DelegateCommand<object> AddUserCommand
        {
            get
            {
                return new DelegateCommand<object>(o =>
                {  // 执行Command绑定事件
                    UserInfo user = new UserInfo()
                    {
                        UserId = 1,
                        UserName = "zhy",
                        UserPwd = "123456",
                    };
                    //_DbContext.Add(company);
                    //_DbContext.SaveChanges();
                    using (ZhyDbContext context = new ZhyDbContext())
                    {
                        context.Add(user);
                        context.SaveChanges();   //提交保存 
                    }

                }, o =>
                { // 执行条件检测事件
                    return true; //决定了目标对象的可用性 true可用 false不可用
                });
            }
        }

        /// <summary>
        /// 查询按钮
        /// </summary>
        public DelegateCommand<object> QueryCommand
        {
            get
            {
                return new DelegateCommand<object>(o =>
                {  // 执行Command绑定事件
                    using (ZhyDbContext context = new ZhyDbContext())
                    {
                        Expression<Func<UserInfo, bool>> predicate;
                       
                        ////查询
                        //Company company1 = context.Company.FirstOrDefault(c => c.Name.Equals("朝夕教育科技有限公司"));
                       var dbUser= context.UserInfo;
                        dbUser.Where(p=>p.UserName=="zyf");
                        var item = dbUser.Where(p => p.UserId == 1).FirstOrDefault();
                    }

                }, o =>
                { // 执行条件检测事件
                    return true; //决定了目标对象的可用性 true可用 false不可用
                });
            }
        }

        /// <summary>
        /// 修改按钮
        /// </summary>
        public DelegateCommand<object> ModifyCommand
        {
            get
            {
                return new DelegateCommand<object>(async o =>
                {  // 执行Command绑定事件
                    using (ZhyDbContext context = new ZhyDbContext())
                    {
                        ////查询
                        Company company1 = context.Company.FirstOrDefault(c => c.Name.Equals("朝夕教育科技有限公司"));
                        company1.CreateTime = DateTime.Now;
                        context.SaveChanges();   //提交保存 
                    }

                }, o =>
                { // 执行条件检测事件
                    return true; //决定了目标对象的可用性 true可用 false不可用
                });
            }
        }

        /// <summary>
        /// 删除按钮
        /// </summary>
        public DelegateCommand<object> DeleteCommand
        {
            get
            {
                return new DelegateCommand<object>(o =>
                {  // 执行Command绑定事件
                    using (ZhyDbContext context = new ZhyDbContext())
                    {
                        ////查询
                        Company company1 = context.Company.FirstOrDefault(c => c.Name.Equals("朝夕教育科技有限公司"));
                        context.Remove(company1);
                        context.SaveChanges();   //提交保存 
                    }

                }, o =>
                { // 执行条件检测事件
                    return true; //决定了目标对象的可用性 true可用 false不可用
                });
            }
        }
    }
}
